﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="App.xaml.cs" company="Microsoft">
//   Microsoft
// </copyright>
// <summary>
//   Defines the App type.
// </summary>
// Copyright (c) Microsoft Corporation. All rights reserved.
// 
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
// ----------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
// --------------------------------------------------------------------------------------------------------------------

namespace MySemanticSearch
{
    using System;
    using System.Windows;
    using System.Windows.Controls;

    /// <summary>
    /// Main App Silverlight Page
    /// </summary>
    public partial class App
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="App"/> class.
        /// </summary>
        public App()
        {
            this.Startup += this.ApplicationStartup;
            this.Exit += ApplicationExit;
            this.UnhandledException += ApplicationUnhandledException;

            InitializeComponent();
        }

        /// <summary>
        /// Method to call at application close
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The event argument.
        /// </param>
        private static void ApplicationExit(object sender, EventArgs e)
        {
        }

        /// <summary>
        /// Method to report an error to the DOM in the browser
        /// </summary>
        /// <param name="e">
        /// The event argument.
        /// </param>
        private static void ReportErrorToDom(ApplicationUnhandledExceptionEventArgs e)
        {
            var errorMsg = e.ExceptionObject.Message + e.ExceptionObject.StackTrace;
            errorMsg = errorMsg.Replace('"', '\'').Replace("\r\n", @"\n");

            System.Windows.Browser.HtmlPage.Window.Eval("throw new Error(\"Unhandled Error in Silverlight Application " + errorMsg + "\");");
        }

        /// <summary>
        /// Method to call when an exception is unhandled
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The event argument.
        /// </param>
        private static void ApplicationUnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
        {
            // If the app is running outside of the debugger then report the exception using
            // the browser's exception mechanism. On IE this will display it a yellow alert 
            // icon in the status bar and Firefox will display a script error.
            if (System.Diagnostics.Debugger.IsAttached)
            {
                return;
            }

            // NOTE: This will allow the application to continue running after an exception has been thrown
            // but not handled. 
            // For production applications this error handling should be replaced with something that will 
            // report the error to the website and stop the application.
            e.Handled = true;
            Deployment.Current.Dispatcher.BeginInvoke(() => ReportErrorToDom(e));
        }

        /// <summary>
        /// Method to call at application startup
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The event argument.
        /// </param>
        private void ApplicationStartup(object sender, StartupEventArgs e)
        {
            var root = new ScrollViewer();
            root.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
            root.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
            root.Content = new MainPage();
            this.RootVisual = root;
        }
    }
}
